package com.hhx.baselibrary.base;

import android.os.Bundle;

import com.hhx.baselibrary.mvp.BasePresenter;
import com.hhx.baselibrary.mvp.BaseView;

/**
 * 父类->基类->动态指定类型->泛型设计（通过泛型指定动态类型->由子类指定，父类只需要规定范围即可）
 */
public abstract class BaseMVPActivity<V extends BaseView, P extends BasePresenter<V>> extends BaseActivity {

    //引用V层和P层
    private P presenter;
    private V view;

    public P getPresenter() {
        return presenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(getLayoutId());
        //ButterKnife.bind(this);
        if (presenter == null) {
            presenter = createPresenter();
        }
        if (view == null) {
            view = createView();
        }
        if (presenter != null && view != null) {
            presenter.attachView(view);
        }
        initPresenter();
    }

    //由子类指定具体类型
    public abstract void initPresenter();

    public abstract P createPresenter();

    public abstract V createView();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
}
